package code.Frame;

import code.MYSQL;
import code.Main;
import code.SHA256;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.regex.Pattern;

public class loginFrame {
    public JPanel loginpanel;
    private JLabel title;
    private JTextField user;
    private JPasswordField password;
    private JButton confirm;
    private JLabel uid;
    private JLabel pw;
    private JButton back;
    private JButton register;

    public loginFrame()
    {
        back.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                Main.frame.remove(Main.panel_now);
                Main.panel_now = FrameManager.startPanel;
                Main.frame.setContentPane(Main.panel_now);
                Main.panel_now.revalidate();
            }
        });
        register.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                Main.frame.remove(Main.panel_now);
                Main.panel_now = FrameManager.regPanel;
                Main.frame.setContentPane(Main.panel_now);
                Main.panel_now.revalidate();
            }
        });
        confirm.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                String Password = String.valueOf(password.getPassword());
                String uid = user.getText();
                if (uid.length() != 9)
                {
                    JOptionPane.showMessageDialog(Main.frame, "账号位数不一致");
                    return ;
                }
                if (!Pattern.compile("[0-9]*").matcher(uid).matches())
                {
                    JOptionPane.showMessageDialog(Main.frame, "账号应为纯数字");
                    return ;
                }
                if (Password.length() < 6 || Password.length() > 20)
                {
                    JOptionPane.showMessageDialog(Main.frame, "密码为6~20位");
                    return ;
                }
                String password_hash = SHA256.getSHA256StrJava(Password);
                int res = -2;
                try
                {
                    res = MYSQL.query_login(Integer.parseInt(uid), password_hash);
                } catch (SQLException ex)
                {
                    throw new RuntimeException(ex);
                }
                switch (res)
                {
                    case 1 -> // 普通用户登录
                    {
                        JOptionPane.showConfirmDialog(Main.frame, "用户登录成功", "", JOptionPane.DEFAULT_OPTION);
                        Main.uid = Integer.parseInt(uid);

                        Main.frame.remove(Main.panel_now);
                        Main.panel_now = FrameManager.user_main;
                        Main.frame.setContentPane(Main.panel_now);
                        Main.panel_now.revalidate();
                    }
                    case 2 -> // 商家登录
                    {
                        JOptionPane.showConfirmDialog(Main.frame, "商家登录成功", "", JOptionPane.DEFAULT_OPTION);
                        Main.uid = Integer.parseInt(uid);

                        Main.frame.remove(Main.panel_now);
                        Main.panel_now = FrameManager.ep_main;
                        FrameManager.ep_mainFrame.start_check();
                        Main.frame.setContentPane(Main.panel_now);
                        Main.panel_now.revalidate();
                    }
                    case 3 -> // 食堂管理员登录
                    {
                        JOptionPane.showConfirmDialog(Main.frame, "食堂管理员登录成功", "", JOptionPane.DEFAULT_OPTION);
                        Main.uid = Integer.parseInt(uid);

                        Main.frame.remove(Main.panel_now);
                        Main.panel_now = FrameManager.admin_main;
                        Main.frame.setContentPane(Main.panel_now);
                        Main.panel_now.revalidate();
                    }
                    case 0 -> // 密码错误
                    {
                        JOptionPane.showMessageDialog(Main.frame, "密码错误");
                        return ;
                    }
                    case -1 -> // 用户不存在
                    {
                        JOptionPane.showMessageDialog(Main.frame, "用户不存在, 请先注册");
                        return ;
                    }
                }
            }
        });
    }
}
